<?php
/**
 * @author Radovan Dvorsky
 * @version 200100312
 */
require_once ('Zend/Form.php');

class Form_Harmonogram_Add extends Zend_Form {
	
	protected $_groups;
	protected $_commissions;	
	
	public function init(){
		$cache = Zend_Registry::get('cache');
		$cacheNameGroups = My_Support::getCacheName('Students','fetchAll');
		$cacheNameCommissions = My_Support::getCacheName('CommissionController','fetchAll');
		
		/*
		 * TODO: pridat podmienku na selectovanie len na urcitych studentov
		 */
		if(!($this->_groups = $cache->load($cacheNameGroups))){
			$groupsModel = new Db_TTridy();
			$this->_groups = $groupsModel->fetchAll();
			$cache->save($this->_groups,$cacheNameGroups);
		}
		
		if(!($this->_commissions = $cache->load($cacheNameCommissions))){
			$commissionModel = new Db_Commission();
			$this->_commissions = $commissionModel->fetchAll();
			$cache->save($this->_commissions,$cacheNameCommissions);
		}		
	}
	
	public function __construct($options = null) {
		parent::__construct ($options);
		
		$this->addDecorator('Fieldset',array('legend' => 'Zostaviť harmonogram'));
		
		$urlHelper = new Zend_View_Helper_Url();
		$studentFillUrl = $urlHelper->url(array(
			'controller' => 'harmonogram',
			'action' => 'fill-student',
			'format' => 'html'
			)
		);
		
		$onStudentSelectChangeFunction = '$.post("' . $studentFillUrl . '",{ID_tr: $(this).val()},' 
									   . 'function(data){$(".message").html(data);'
									   . 	'$("select#ID_st").html(data);'
									   . '},"html");';
		
											   
		$groupSelect = new Zend_Form_Element_Select('groupSelect');
		$groupSelect->setLabel('Odbor:')
		->addMultiOption('-','Vyberte si:');
		$groupSelect->setAttrib('onchange',$onStudentSelectChangeFunction);
		
		foreach($this->_groups as $group){
			$groupSelect->addMultiOption($group->ID_tr,$group->Zkratka);			
		}
						
		$studentSelect = new Zend_Form_Element_Select('ID_st');
		$studentSelect->setLabel('Meno skúšaného:')
		->setRegisterInArrayValidator(false);
		
		$commissionSelect = new Zend_Form_Element_Select('cid');
		$commissionSelect->setLabel('Komisia:');
		foreach($this->_commissions as $commission){
			$commissionSelect->addMultiOption($commission->cid,$commission->name);
		}
						
		$jqueryParams = array(
			'dateFormat' => 'dd-mm-yy',
			'dayNames' => array(
				'Nedeľa', 'Pondelok', 'Utorok', 'Streda', 'Štvrtok', 'Piatok', 'Sobota'
			),
			'dayNamesMin' => array(
				'Ne', 'Po', 'Ut', 'St', 'Št', 'Pi', 'So'
			),
			'duration' => 'fast',
			'firstDay' => 1,
			'monthNames' => array(
				'Január','Február','Marec','Apríl','Máj','Jún','Júl','August','September','Október','November','December'
			)
		);
		
		$block1 = new ZendX_JQuery_Form_Element_DatePicker("block1");
		$block1->setLabel('Odborný blok 1:')
		->setJqueryParams($jqueryParams);
		
		$block2 = new ZendX_JQuery_Form_Element_DatePicker("block2");
		$block2->setLabel('Odobrný blok 2:')
		->setJqueryParams($jqueryParams);
		
		$bcdate = new ZendX_JQuery_Form_Element_DatePicker("bcdate");
		$bcdate->setLabel('Obhajoba bc. práce:')
		->setJqueryParams($jqueryParams);
		
		
		$this->addElements(array($groupSelect,$studentSelect,$commissionSelect,$block1,$block2,$bcdate));	
	}
}